package com.qf.commons.web.aop;

import com.qf.commons.web.utils.HttpUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取网关层 用户身份解析的 切面
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@Aspect
public class AuthInfoAspect {

    @Around("@within(org.springframework.web.bind.annotation.RestController))")
    public Object authAround(ProceedingJoinPoint joinPoint) throws Throwable {

        //获取请求对象
        HttpServletRequest request = HttpUtils.getRequest();
        String uidStr = request.getHeader("uid");

        if (uidStr == null) {
            return joinPoint.proceed();
        }

        //转换成Long类型
        Long uid = Long.parseLong(uidStr);
        //放入ThreadLocal里
        UserInfo.setUid(uid);
        try {
            return joinPoint.proceed();
        } finally {
            //清空ThreadLocal
            UserInfo.clean();
        }
    }
}
